import "@/assets/icons/iconfont/iconfont.js";
import "@/assets/styles/tailwind.css";
import RenDeptTree from "@/components/ren-dept-tree";
import RenRadioGroup from "@/components/ren-radio-group";
import RenRegionTree from "@/components/ren-region-tree";
import RenSelect from "@/components/ren-select";
import ElementPlus from "element-plus";
import "element-plus/theme-chalk/display.css";
import "element-plus/theme-chalk/index.css";
import locale from "element-plus/es/locale/lang/zh-cn";
import localeEn from "element-plus/es/locale/lang/en";
import { createApp } from "vue";
import { createPinia } from "pinia";
import App from "./App.vue";
import router from "./router";
import * as ElementPlusIcons from "@element-plus/icons-vue";
import 'element-plus/dist/index.css';
import { permission, role } from '@/utils/permission';

import axios from "axios";
import "virtual:svg-icons-register";
import { initTimeSync, updateOffsetFromServer } from "@/utils/timeSync";
import { timeApi } from "@/api/time";

const app = createApp(App);
const pinia = createPinia();
Object.keys(ElementPlusIcons).forEach((iconName) => {
  app.component(iconName, ElementPlusIcons[iconName as keyof typeof ElementPlusIcons]);
});
app
  .directive("permission", permission)
  .directive("role", role);
app
  .use(createPinia())
  .use(router)
  .use(RenRadioGroup)
  .use(RenSelect)
  .use(RenDeptTree)
  .use(RenRegionTree)
  .use(ElementPlus, { size: "default", locale: localeEn })
  .mount("#app");

window.axios = axios;

// 应用启动后做一次时间同步（骨架：不影响功能，失败也不阻塞）
(async () => {
  try {
    await initTimeSync();
    const res: any = await timeApi.getCurrent();
    const serverNow = (res?.data?.now ?? res?.data ?? 0) as number;
    if (serverNow) {
      updateOffsetFromServer(Number(serverNow));
    }
  } catch (e) {
    console.warn("time sync skipped:", e);
  }
})();
